package com.android.dumiaogou.view.dialog;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;

import com.android.dumiaogou.utils.ObjectUtils;
import com.android.dumiaogou.R;
import com.android.dumiaogou.model.MainTabData;

import java.util.ArrayList;


/**
 * Created by Administrator on 2016/11/16.
 */

public class MainMsg extends PopupWindow implements View.OnClickListener {


    private ArrayList<MainTabData> names = new ArrayList<>();


    private PopupWindow popupWindow;
    private OnSetSureClickListener surelistener;

    private Activity context;


    private final View view;
    private View ll_poplist;
    private ListView ll_poplist_list;


    public interface OnSetSureClickListener {
        public void onSure();
    }

    public void setOnSetSureClickListener(OnSetSureClickListener listener) {
        this.surelistener = listener;
    }


    private OnTaktPhotoClickListener listener;

    public interface OnTaktPhotoClickListener {
        public void onClickCamera();

        public void onClickGallery();
    }

    public void setOnSetCameraClickListener(OnTaktPhotoClickListener listener) {
        this.listener = listener;
    }

    private OnPopListDataClickListener poplist_listener;

    public interface OnPopListDataClickListener {
        void onData(String name, String id);
    }

    public void setPopListDataClickListener(OnPopListDataClickListener listener) {
        this.poplist_listener = listener;
    }

    public MainMsg(Activity context) {
        this.context = context;

        view = LayoutInflater.from(context).inflate(R.layout.dialog_message, null);
        popupWindow = new PopupWindow(view, LinearLayoutCompat.LayoutParams.MATCH_PARENT,
                LinearLayoutCompat.LayoutParams.MATCH_PARENT, true);
        popupWindow.setAnimationStyle(R.style.popWindow_anim_style);
        View close = view.findViewById(R.id.close);
        close.setOnClickListener(this);


        //


    }


    private String tv = "";

    public void show(View parent, int type, String tv) {
        this.tv = tv;
        Show(type);
        popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.update();

    }

    private String thumb = "";

    public void show(View parent, int type, String tv, String thumb) {
        this.tv = tv;
        this.thumb = thumb;
        Show(type);
        popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.update();

    }

    private void Show(final int type) {
        switch (type) {

            case 0:
                View dialog_ll = view.findViewById(R.id.dialog_ll);
                TextView dialog_ll_text = (TextView) view.findViewById(R.id.dialog_ll_text);
                view.findViewById(R.id.dialog_ll_canel).setOnClickListener(this);
                view.findViewById(R.id.dialog_ll_yes).setOnClickListener(this);
                if (tv != null && !tv.equals("")) {
                    dialog_ll.setVisibility(View.VISIBLE);
                    dialog_ll_text.setText(tv);
                }
                break;

            case 1:
                View takephoto_ll = view.findViewById(R.id.takephoto_ll);
                view.findViewById(R.id.photo_camera).setOnClickListener(this);
                view.findViewById(R.id.photo_gallery).setOnClickListener(this);
                view.findViewById(R.id.photo_cancel).setOnClickListener(this);
                takephoto_ll.setVisibility(View.VISIBLE);


                break;
            case 2:
                ll_poplist = view.findViewById(R.id.ll_poplist);
                ll_poplist_list = (ListView) view.findViewById(R.id.ll_poplist_list);
                names.clear();
                names.add(new MainTabData("仅退款", "1"));
                names.add(new MainTabData("退货退款", "2"));
                ll_poplist.setVisibility(View.VISIBLE);
                final Adapt adapt = new Adapt(names);
                ll_poplist_list.setAdapter(adapt);
                ll_poplist_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                        adapt.select(position);
                        poplist_listener.onData(names.get(position).getName(), names.get(position).getId());
                    }
                });
                break;
            case 3:
                ll_poplist = view.findViewById(R.id.ll_poplist);
                ll_poplist_list = (ListView) view.findViewById(R.id.ll_poplist_list);
                names.clear();
                names.add(new MainTabData("请选择退货退款原因", "0"));
                names.add(new MainTabData("其他", "90"));
                names.add(new MainTabData("效果不好不喜欢", "95"));
                names.add(new MainTabData("商品破损，有污渍", "96"));
                names.add(new MainTabData("保质期不符", "97"));
                names.add(new MainTabData("认为是假货", "98"));
                names.add(new MainTabData("不能按时发货", "99"));
                ll_poplist.setVisibility(View.VISIBLE);
                final Adapt adapt_result = new Adapt(names);
                ll_poplist_list.setAdapter(adapt_result);
                ll_poplist_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                        adapt_result.select(position);
                        poplist_listener.onData(names.get(position).getName(), names.get(position).getId());

                    }
                });
                break;
            case 4:
                View ll_qrcode = view.findViewById(R.id.ll_qrcode);
                TextView name_qrcode = (TextView) view.findViewById(R.id.name_qrcode);
                ImageView img_qrcode = (ImageView) view.findViewById(R.id.img_qrcode);
                if (tv != null && !tv.equals("")) {
                    ll_qrcode.setVisibility(View.VISIBLE);
                    name_qrcode.setText(tv);
                    ObjectUtils.photo(context, thumb, img_qrcode);
                }
                break;

            case 5:
                ll_poplist = view.findViewById(R.id.ll_poplist);
                ll_poplist_list = (ListView) view.findViewById(R.id.ll_poplist_list);
                names.clear();
                names.add(new MainTabData("微信", "1"));
                names.add(new MainTabData("支付宝", "2"));
                ll_poplist.setVisibility(View.VISIBLE);
                final Adapt adapt_cash = new Adapt(names);
                ll_poplist_list.setAdapter(adapt_cash);
                ll_poplist_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                        adapt_cash.select(position);
                        poplist_listener.onData(names.get(position).getName(), names.get(position).getId());
                    }
                });
                break;

            case 6:
                View ll_login = view.findViewById(R.id.ll_login);
                view.findViewById(R.id.phone_login).setOnClickListener(this);
                view.findViewById(R.id.qq_login).setOnClickListener(this);
                view.findViewById(R.id.sina_login).setOnClickListener(this);
                view.findViewById(R.id.canel).setOnClickListener(this);
                ll_login.setVisibility(View.VISIBLE);
                break;
            case 7:
                view.findViewById(R.id.ll_liucheng).setVisibility(View.VISIBLE);
                break;

        }

    }


    public void close() {

        if (popupWindow != null && popupWindow.isShowing()) {

            popupWindow.dismiss();


        }


    }

    private OnLoginStyleClickListener login_listener;

    public interface OnLoginStyleClickListener {
        void LoginPhone();

        void LoginQQ();

        void LoginSina();
    }

    public void setOnSetLoginStyleClickListener(OnLoginStyleClickListener login_listener) {
        this.login_listener = login_listener;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.dialog_ll_canel:
                close();
                break;
            case R.id.dialog_ll_yes:
                surelistener.onSure();
                break;
            case R.id.close:
                close();
                break;
            case R.id.photo_camera:
                listener.onClickCamera();
                break;
            case R.id.photo_gallery:
                listener.onClickGallery();
                break;
            case R.id.sina_login:
                login_listener.LoginSina();
                break;
            case R.id.qq_login:
                login_listener.LoginQQ();
                break;
            case R.id.phone_login:
                login_listener.LoginPhone();
                break;
            case R.id.photo_cancel:
                close();
                break;
            case R.id.canel:
                close();
                break;


        }
    }

    private int pospay;

  private   class Adapt extends BaseAdapter {

        ArrayList<MainTabData> address;

        public Adapt(ArrayList<MainTabData> listProvince) {
            this.address = listProvince;
        }

        public void select(int position) {
            pospay = position;
            notifyDataSetChanged();

        }

        @Override
        public int getCount() {


            return address.size();
        }


        @Override
        public Object getItem(int position) {
            return null;
        }


        @Override
        public long getItemId(int position) {
            return 0;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View inflate_paystyle = LayoutInflater.from(context).inflate(R.layout.dialog_message_item, null);
            TextView pay_name = (TextView) inflate_paystyle.findViewById(R.id.pay_name);
            RadioButton pay_check = (RadioButton) inflate_paystyle.findViewById(R.id.item_list_mine_pay_check);
//            pay_check.setVisibility(View.VISIBLE);
            if (pospay == position) {//最后选择的位置
                pay_check.setChecked(true);

            } else {
                pay_check.setChecked(false);

            }
            pay_name.setText(address.get(position).getName());

            return inflate_paystyle;
        }
    }

}
